<?php

/* @project : Template
 * @auther  : 青云
 * @date    : 2018/1/10
 * @desc    : 系统设置
 */

namespace app\admin\controller;

use think\Request;

use app\common\model\Excel;

class SystemConfigTab extends Base
{
    function add()
    {
        return $this->fetch();
    }

    function index(Request $request)
    {        
        $request->_sort = 'sort asc';
        return $this->_index($request);
    }

    function del(Request $request)
    {
        $result = $this->_del($request);
        if ($result) {
            return $this->api_success('删除成功');
        } else {
            return $this->api_error('删除失败');
        }
    }

    function is_top($id)
    {
        $article = ArticleModel::find($id);

        $this->assign('article', $article);
        return $this->fetch();
    }

    function tag(Request $request)
    {
        $tag_model = new TagModel();
        $tag_list  = $tag_model->getList();

        $this->assign('tag_list', $tag_list);

        $tag_id_list = $tag_model->getTagIdListByRowId($request->id, $request->controller());

        $this->assign('tag_id_list', $tag_id_list);

        $vo = ArticleModel::find($request->id);

        $this->assign('vo', $vo);

        return $this->fetch();
    }

    function set_tag(Request $request)
    {
        $tag_model = new TagModel();
        $result    = $tag_model->setTag($request);

        if ($result != false) {
            return $this->api_success('操作成功');
        } else {
            return $this->api_success('操作失败');
        }
    }

    function export_to_excel()
    {
        $excel = new Excel();

        $excel->load('tpl/tpl_data.xlsx');

        $cols[] = array('title' => '标题', 'field' => 'name');
        $cols[] = array('title' => '分类', 'field' => 'name');

        $cols[] = array('title' => '作者', 'field' => 'author');
        $cols[] = array('title' => '发布日期', 'field' => 'post_time');

        $excel->cols($cols);

        $article_model = new ArticleModel();
        $data          = $article_model->select()->toArray();

        $excel->data($data);
        $date = date('Y-m-d H:i:s');
        $excel->download("文章_{$date}.xlsx");
    }

    function upload(Request $request)
    {
        return $this->_upload($request);
    }

    function download($sid)
    {
        return $this->_download($sid);
    }

    function set_valid(Request $request)
    {
        return $this->_set_valid($request);
    }

    function set_invalid(Request $request)
    {
        return $this->_set_invalid($request);
    }
}
